1. Field of the Invention
This invention relates in general to a computer operating systems, and more particularly to a method, apparatus and program storage device for preserving locked pages in memory when in user mode.
2. Description of Related Art
A thread is a sequence of instructions. Multiple threads can be executed in parallel on many computer systems. Multithreading generally occurs by time slicing where a single processor switches between different threads or by multiprocessing where threads are executed on separate processors. Threads are similar to processes, but differ in the way that they share resources. Many modern operating systems directly support both time-sliced and multiprocessor threading with a process scheduler. The operating system kernel allows programmers to manipulate threads via the system call interface.
Operating systems generally implement threads in one of two ways: preemptive multithreading, or cooperative multithreading. Preemptive multithreading allows the operating system to determine when a context switch should occur. The term context simply refers to all the information that completely describes the process's current state of execution, e.g., the contents of the CPU registers, the program counter, the flags, etc. The process of saving the currently executing process's context, flushing the CPU, and loading the next process's context, is called a context switch. A context switch for a full-fledged, multithreaded process will obviously take a lot longer than a context switch for an individual thread within a process. So depending on the amount of hardware support for context switching and the type of context switch, a context switch can take a decent amount of time, thereby costing a number of CPU cycles. Cutting back on context switches improves execution efficiency and reduces delays, as does the extensive use of multithreading since thread switches are usually faster than full-sized process switches.
Cooperative multithreading, on the other hand, relies on the threads themselves to relinquish control once they are at a stopping point. This can create problems if a thread is waiting for a resource to become available. The disadvantage to preemptive multithreading is that the system may make a context switch at an inappropriate time, causing priority inversion or other bad effects if there are real-time requirements at the application level, which may be avoided by cooperative multithreading.
In multithreaded operating systems, all threads in a task share the same address space and are usually sharing large amounts of data. Context switching between two threads in the same task may take much less time than context switching between two threads in different tasks. After a context switch in a multithreaded system a new thread will be running, and perhaps a new task.
The kernel mode is a privileged mode in which only the kernel runs and which provides access to all memory locations and all other system resources. Context switches can occur only in kernel mode. Other programs, including applications, initially operate in user mode, but they can run portions of the kernel code via system calls. A system call is a request by an active process for a service performed by the kernel, such as input/output (I/O) or process creation.
To implement a real-time application in user mode, it is highly desirable to have the text and data required by the application always reside in the memory. For a typical user mode application, text and data are paged in as and when needed. Memory is divided into a number of fixed size or variable sized partitions or regions that are allocated to running processes. The variable size partition scheme may result in a situation where available memory is not contiguous, but fragmented into many scattered blocks. To solve this problem, the memory may be compacted thereby making large free memory blocks or a paging scheme may be used, which allows a program's memory to be noncontiguous thereby permitting a program to be allocated physical memory wherever it is available.
However, paging schemes influence process scheduling. When a process is to be executed, the scheduler checks its size expressed in pages. Available memory is examined for that process. If the process requires N pages, there must be N frames available in memory that must be allocated to this process before the process can be executed. Thus, when the code is running there will be cases where the underlying operating system has to stop executing the process code and wait for the text or data to be paged into the main memory. This is done in a transparent method that a user application is not aware that it is waiting on a page. However, it does cause unexpected delays and context switching which are very expensive in a tightly time bound process.
This issue is circumvented by loading and locking in the memory all the code and data that a real-time application is going to require during the initial bring-up of the process. However, if at any point of time the process gets context switched out after the initial bring-up process, problems may occur in the system due to the delays in context switching and the unexpected changes of timing among interacting processes inside the real-time application with stringent timing requirements.
There is also a potential problem when a process is running in user mode. If at any point of time the process calls “system(cmd)” to run command ‘cmd’, that translates to forking a process and making the process do the work related to ‘cmd’. Each process has process context, which is everything that is unique about the state of the program you are currently running. A fork in a multithreading environment means that a thread of execution is duplicated. When a new process is forked, it shares relatively little data with the parent process that created it; when a new thread is created, it shares much more information (such as all the global variables and static local variables, the open files, and the process ID). Specifically in an operating system like Linux, whenever a process does a fork, it changes the property of all the memory pages used by the process to “copy on write”. This unlocks the memory pages previously locked by the process and can cause the operating system to page out or drop these unlocked memory pages when the system comes under memory stress. As a result, a real-time application will go through the undesired re-loading of the memory pages later during its execution even when it has attempted to “lock” the memory pages in the first place during the initialization. A temporary suspension of program execution due to the delays when a required memory page is being brought in can introduce problems to a real-time application.
It can be seen then that there is a need for a method, apparatus and program storage device for preserving locked pages in memory when in user mode.